<?php
namespace Bundle\FireIceBundle\Command;

use Zend\Paginator;

use Symfony\Component\Console\Output;

use Symfony\Component\Console\Command\Command as BaseCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Util\Filesystem;
use Symfony\Bundle\FrameworkBundle\Util\Mustache;

class BuildModuleRoutesCommand extends BaseCommand
{
    protected function configure()
    {
        $this->setDefinition(array(
            new InputOption('nodeid','', InputOption::PARAMETER_REQUIRED, 'Id of node to build routing. `-1` means full sitetree.', '-1'),
        ));
        $this->setName('fireice:construct:routes');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $target = $input->getOption('nodeid');
        if(strpos($target, ',') !== false)
        {
            if(strpos($target, '-1'))
            {
                $target = array(-1);
            }
            else
            {
                $target = explode(',', $target);
            }
        }
        else
        {
            $target = array((int) $target);
        }

        $output->write('Building tree for `');
        $output->setDecorated(true);
        $output->write(implode(', ', $target));
        $output->setDecorated(false);
        $output->write('`.', true);

        $container = $this->application->getKernel()->getContainer();
        try
        {
	        $repo = $container->get('doctrine.orm.default_entity_manager')->
	                    getRepository('Bundle\TreeBundle\Entities\Tree');
	
	        if($target[0] == -1)
	        {
	            $targetModels = $repo->findAll();
	        }
	        else
	        {
	            throw new Exception(sprintf("Not implemented %s:%d | %s::%s", __FILE__, __LINE__, __CLASS__, __METHOD__));
	            $targetModels = $repo->find($target);
	        }
	        $output->write(sprintf('Building routing for %d nodes.', count($targetModels)), true);
        }
        catch (\Exception $e)
        {
        	$output->write($e->getMessage(), true);
        	throw $e;
        }

        foreach($targetModels as $model)
        {
            $nodeRouting = $container->get('fireice')->getDynamicRouter()->
                buildRoutingForNode($model);
        }
        
        // phpfile code
        $code = $container->get('fireice')->getControllerBuilder()->
                    getCode(false);
                    
        $fcache = $container->get('fireice')->getFileCache();

        $controller = $container->getParameter('fireice.route_generator.controller');
        $serialized = $container->getParameter('fireice.route_generator.collection_ser');
        
        $output->write(sprintf('Target folder `%s`', $fcache->getTargetDirectory()), true);
        $output->write(sprintf('Generated controller `%s%s%s`', $fcache->getTargetDirectory(), DIRECTORY_SEPARATOR , $controller), true);
        
        $fcache->store($controller, $code);

        $output->write(sprintf('Writing generated routing collection to `%s%s%s`', $fcache->getTargetDirectory(), DIRECTORY_SEPARATOR ,$serialized), true);
        
        $fcache->store($serialized, serialize($container->get('fireice')->getDynamicRouter()->getRouteCollection()));

        $output->write("Done.", true);
    }
}